package aa.db;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.*;

@WebServlet("/payment")
public class PaymentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        // 获取参数
        String tableId = request.getParameter("table_id");
        String restaurantId = request.getParameter("restaurant_id");

        // 构建SQL更新语句
        String updateSQL = "UPDATE shoppingcar SET pay='已支付' WHERE table_id=? AND restaurant_id=? AND pay='未支付'";
        
        try (Connection conn = DriverManager.getConnection("jdbc:mariadb://10.220.140.102:3366/studb11","stua","zHZUA65r");
             PreparedStatement pstmt = conn.prepareStatement(updateSQL)) {
            
            pstmt.setString(1, tableId);
            pstmt.setString(2, restaurantId);

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();
            
            if (rowsAffected > 0) {
                // 更新成功，重定向到ok.jsp
                response.sendRedirect("ok.jsp");
            } else {
                // 更新失败或无记录被更新，重定向到error.jsp
                response.sendRedirect("error1.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理异常，重定向到error.jsp
            response.sendRedirect("error1.jsp");
        }
    }
}